<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>08 - Vector3 设置顶点数据</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
    </style>
    <!-- 引入三维引擎 -->
    <script src="../external/three-js-r93/build/three.js"></script>
    <!-- 扩展控件 OrbitControls -->
    <script src="../external/three-js-r93/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<script>

    let scene = new THREE.Scene();

    let geometry = new THREE.Geometry();

    let p1 = new THREE.Vector3(0, 0, 0);
    let p2 = new THREE.Vector3(10, 50, 0);
    let p3 = new THREE.Vector3(40, 50, 0);
    let p4 = new THREE.Vector3(30, 0, 0);

    geometry.vertices.push(p1, p2, p3, p4);

    /* 定义顶点颜色 */
    let color1 = new THREE.Color(0xff0000);
    let color2 = new THREE.Color(0xffff00);
    let color3 = new THREE.Color(0x00ff00);
    let color4 = new THREE.Color(0x00ffff);

    geometry.colors.push(color1, color2, color3, color4);

    let material = new THREE.MeshBasicMaterial({vertexColors: THREE.VertexColors});

    let line = new THREE.Line(geometry, material);

    scene.add(line);

    let pointsMaterial = new THREE.PointsMaterial({
        // color:0xff0000,
        vertexColors:THREE.VertexColors,
        size:5.0
    });

    let points = new THREE.Points(geometry, pointsMaterial);
    scene.add(points);


    /* 添加辅助坐标 */
    let axesHelper = new THREE.AxesHelper(200);
    scene.add(axesHelper);

    //点光源
    var point = new THREE.PointLight(0xffffff);
    point.position.set(400, 200, 300); //点光源位置
    scene.add(point); //点光源添加到场景中
    //环境光
    var ambient = new THREE.AmbientLight(0x444444);
    scene.add(ambient);

    /* 添加相机 */
    let width = window.innerWidth;
    let height = window.innerHeight;
    let k = width / height; // 窗口宽高比
    let s = 150; // 三维场景显示范围控制系数， 系数越大，显示的范围越大

    let camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
    camera.position.set(200, 300, 200);
    camera.lookAt(scene.position);

    /* 渲染器 */
    let renderer = new THREE.WebGLRenderer({antialis: true});// 抗锯齿
    renderer.setSize(width, height);//渲染区域尺寸
    renderer.setClearColor(0xb9d3ff, 1);//渲染背景色

    document.body.appendChild(renderer.domElement); // body 元素中插入 canvas 对象

    /*渲染函数*/
    function render() {

        renderer.render(scene, camera);

    }

    render();

    /* 添加控件对象 */
    let controls = new THREE.OrbitControls(camera, renderer.domElement);// 控件可以监听鼠标的变化，改变相机的属性
    controls.addEventListener('change', render);//添加鼠标监听，触发渲染函数，更新 canvas 画布渲染效果

</script>

</body>
</html>